package com.example.venue_reservation_service.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.venue_reservation_service.domain.Type;
import com.example.venue_reservation_service.dto.PageDTO;
import com.example.venue_reservation_service.service.TypeService;
import com.example.venue_reservation_service.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Optional;

@RestController
@RequestMapping("/type")
@CrossOrigin
@Api("场馆管理接口")
public class TypeController {

    @Resource
    private TypeService typeService;

    @Value("${access.url}")
    private String imgPrefix;

    @ApiOperation("查询某个场馆信息")
    @GetMapping("/select/{id}")
    public Result select(@PathVariable("id") Integer id){
        Type type = typeService.getById(id);
        if(Optional.ofNullable(type).isPresent() && StringUtils.isNotBlank(type.getImgUrl())){
            type.setImgUrl(imgPrefix + type.getImgUrl());
        }
        return Result.ok(type).message("查询成功");
    }

    @ApiOperation("查看所有场馆信息")
    @PostMapping("/queryAll")
    public Result queryAll(@RequestBody PageDTO dto){
        return typeService.queryTypes(dto);
    }

    @ApiOperation("添加场馆信息")
    @PostMapping("/create")
    public Result create(@RequestBody Type type){
        return typeService.addType(type);
    }

    @ApiOperation("获取场馆照片资源数据")
    @GetMapping("/data")
    public String data(@RequestParam("tid")Integer tid){
        return typeService.loadImg(tid);
    }

    @ApiOperation("上传场馆照片")
    @PostMapping("/upload")
    public Result upload(@RequestParam("id") Integer id,
                         @RequestParam("file")MultipartFile file){

        return typeService.upload(id, file);
    }

    @ApiOperation("修改场馆信息")
    @PostMapping("/update")
    public Result update(@RequestBody Type type){
        return typeService.updateType(type);
    }

    @ApiOperation("加载场馆列表")
    @GetMapping("/name")
    public Result name(){
        return typeService.getNames();
    }

    @ApiOperation("移除场馆信息")
    @GetMapping("/remove")
    public Result remove(@RequestParam("id")Integer typeId){
        return typeService.delType(typeId);
    }
}
